﻿using Rvz.Game.Main.Entities.Zombies;
using Rvz.Game.Main.State;

namespace Rvz.Game.Main.Rules.Waves
{
    public static class WaveManager
    {
        public static void ProcessWaveEvents(GameState gameState)
        {
            var waveState = gameState.WaveState;

            SpawnZombiesIfNeeded(gameState);
            ZombieManager.ActivateZombies(gameState, waveState.ActiveZombies);
            FinalwaveCheck(gameState);
        }

        private static void SpawnZombiesIfNeeded(GameState gameState)
        {
            var waveState = gameState.WaveState;
            var numberOfZombiesToSpawn = waveState.GetZombieSpawnCount();
            for (var i = 0; i < numberOfZombiesToSpawn; i++)
            {
                var zombie = ZombieManager.SpawnZombie(gameState);
                waveState.AddZombie(zombie);
            }
        }

        private static void FinalwaveCheck(GameState gameState)
        {
            var waveState = gameState.WaveState;
            if (!waveState.IsWaveComplete())
            { return; }

            ProjectileEngine.Reset();
            var nextWave = waveState.NextWave();
            gameState.WaveState = nextWave;

            //TODO: Transition to a purchase upgrades screen, which should then reset the player's position on the new wave
            gameState.PlayerState.Robot.X = 0;
            gameState.PlayerState.Robot.Y = 0;
        }

        public static void FragZombie(GameState gameState, Zombie zombie)
        {
            var waveState = gameState.WaveState;
            waveState.FragZombie(zombie);
        }
    }
}
